/*
 * Created by JFormDesigner on Sun Dec 01 14:42:42 CST 2024
 */

package com.hafu.view.RuKu;

import com.hafu.model.RuKuMain;
import com.hafu.service.RuKuService;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author Brainrain
 */
public class QueryRuKuFrm<rukuService> extends JFrame {
    private RuKuService rukuService = new RuKuService();

    public QueryRuKuFrm() {
        initComponents();
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.setRowCount(0);
        ArrayList<RuKuMain> rukuMainList = rukuService.getRuKuList(new RuKuMain());
        for(RuKuMain ruku:rukuMainList){
            Object[] row = new Object[]{ruku.getId(),ruku.getRuKuName(),ruku.getPrice(),ruku.getGysName(),ruku.getRkDate()};
            defaultTableModel.addRow(row);
        }
    }
    private void button1(ActionEvent e) {
        // TODO add your code here
        String text = textField1.getText();
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.setRowCount(0);
        RuKuMain rukuMain = new RuKuMain();
        rukuMain.setRuKuName(text);
        ArrayList<RuKuMain> rukuMainList = rukuService.getRuKuList(rukuMain);
        for(RuKuMain ruku:rukuMainList){
            Object[] row = new Object[]{ruku.getId(),ruku.getRuKuName(),ruku.getPrice(),ruku.getGysName(),ruku.getRkDate()};
            defaultTableModel.addRow(row);
        }
    }





    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here获取选中行
        int row = table1.getSelectedRow();
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtn.getValueAt(row, 0);
        String name = (String) dtn.getValueAt(row, 1);
        textField2.setText(name);

    }



    private void button2(ActionEvent e) {
        // TODO add your code here
        String name = textField2.getText();
        int row = table1.getSelectedRow();

        // 检查是否有行被选中
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请先选择一行数据");
            return;
        }

        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        RuKuMain rukuMain = new RuKuMain();
        rukuMain.setId(chooseId);
        rukuMain.setRuKuName(name);
        Boolean aBoolean = rukuService.updateRuKu(rukuMain);
        if (aBoolean) {
            JOptionPane.showMessageDialog(null, "修改成功");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        textField2 = new JTextField();
        button2 = new JButton();

        //======== this ========
        setTitle("\u67e5\u8be2\u5165\u5e93\u5355");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5165\u5e93\u5355\u540d\u5b57");

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> {
            button1(e);
            button1(e);
        });

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                    },
                    new String[] {
                            "\u7f16\u53f7", "\u540d\u5b57", "\u603b\u4ef7", "\u4f9b\u5e94\u5546\u540d\u5b57", "\u4e0b\u5355\u65f6\u95f4"
                    }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u5165\u5e93\u5355\u540d\u5b57");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> {
            button2(e);
            updateRKbutton2(e);
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 408, GroupLayout.PREFERRED_SIZE)
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(label1)
                                                .addGap(62, 62, 62)
                                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                                                .addGap(57, 57, 57)
                                                .addComponent(button1, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(label2)
                                                .addGap(65, 65, 65)
                                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)
                                                .addGap(46, 46, 46)
                                                .addComponent(button2)))
                                .addContainerGap(237, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(25, 25, 25)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label1)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button1))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 281, GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label2)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button2))
                                .addGap(84, 84, 84))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    private void updateRKbutton2(ActionEvent e) {
        String name = textField2.getText();
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
